package com.bw.liuyushan.liuyushan20210929.MVP.Activity;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alipay.sdk.app.EnvUtils;
import com.alipay.sdk.app.PayTask;
import com.blankj.utilcode.util.ToastUtils;
import com.bw.liuyushan.liuyushan20210929.Adapter.GoodCarAdapter;
import com.bw.liuyushan.liuyushan20210929.GreenDao.GoodBean;
import com.bw.liuyushan.liuyushan20210929.MVP.Contrack.IContrackTest;
import com.bw.liuyushan.liuyushan20210929.MVP.Model.BaseModelTest;
import com.bw.liuyushan.liuyushan20210929.MVP.Presenter.BasePresenterTest;
import com.bw.liuyushan.liuyushan20210929.R;
import com.bw.liuyushan.liuyushan20210929.Utils.GoodBeanUtils;
import com.bw.liuyushan.liuyushan20210929.zhifubao.PayResult;
import com.bw.liuyushan.liuyushan20210929.zhifubao.util.OrderInfoUtil2_0;
import com.bw.liuyushan.mvp_lib.View.BaseActivity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.List;
import java.util.Map;

/**
 * 购物车页面 单选 全选 价格加减 支付宝
 */
public class GoodCarActivity extends BaseActivity<BasePresenterTest> implements IContrackTest.C_ViewTest, OnRefreshListener {


    private com.scwang.smart.refresh.layout.SmartRefreshLayout smCar;
    private androidx.recyclerview.widget.RecyclerView rvCar;
    private android.widget.CheckBox cbOne;
    private android.widget.TextView allprice;
    private android.widget.Button pay;
    GoodCarAdapter adapter;
    boolean isAdapter;
    List<GoodBean> list;
    int money = 0;


    @Override
    public int bindLayout() {
        return R.layout.activity_car;
    }

    @Override
    public void initView() {
        //链接 防内存泄露
        mPresenter = new BasePresenterTest(new BaseModelTest(), this);

        smCar = (SmartRefreshLayout) findViewById(R.id.sm_car);
        rvCar = (RecyclerView) findViewById(R.id.rv_car);
        cbOne = (CheckBox) findViewById(R.id.cb_one);
        allprice = (TextView) findViewById(R.id.allprice);
        pay = (Button) findViewById(R.id.pay);
        smCar.setOnRefreshListener(this);
        rvCar.setLayoutManager(new LinearLayoutManager(this));
    }

    @Override
    public void initDate() {
        list = GoodBeanUtils.getInstance().getDaoSession().getGoodBeanDao().queryBuilder().list();
        initAdapter(list);
        //支付
        pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtils.showShort("支付");
                //点击事件
                EnvUtils.setEnv(EnvUtils.EnvEnum.SANDBOX);
                payV2();
            }
        });
        //全选
        cbOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (cbOne.isChecked()) {
                    for (GoodBean goodBean : list) {
                        goodBean.setGetCheacks(true);
                        money += Integer.parseInt(goodBean.getPrice());

                    }
                    allprice.setText(String.valueOf(money));
                } else {
                    for (GoodBean goodBean : list) {
                        goodBean.setGetCheacks(false);
                        money = 0;
                    }
                    allprice.setText(String.valueOf(money));
                }
                adapter.notifyDataSetChanged();
            }
        });
    }

    public void getNum() {//逐一单选 全选自动亮起 反之取消的判断方法
        int num = 0;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getGetCheacks()) {
                num++;
                if (num == list.size()) {
                    cbOne.setChecked(true);
                } else {
                    cbOne.setChecked(false);
                }
            }
        }

    }
    private void initAdapter(List<GoodBean> list) {
        smCar.finishRefresh();
        if (adapter == null) {
            adapter = new GoodCarAdapter(list);
            rvCar.setAdapter(adapter);
            //适配器的子控件点击事件
            adapter.setOnItemChildClickListener(new OnItemChildClickListener() {
                @Override
                public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                    if (view.getId() == R.id.cb) {//单选
                        list.get(position).setGetCheacks(!list.get(position).getGetCheacks());
                        getNum();//注意调用位置 位置不对 逻辑不一样 运行效果有问题
                        if (list.get(position).getGetCheacks()) {
                            money += Integer.parseInt(list.get(position).getPrice());
                        } else {
                            money -= Integer.parseInt(list.get(position).getPrice());
                        }

                        allprice.setText(String.valueOf(money));
                    } else if (view.getId() == R.id.pay) {//判断是哪个子控件 switch->case实现无效果 用了if

                    }
                }
            });
        } else {
            if (isAdapter) {
                adapter.getData().clear();
            }
            adapter.getData().addAll(list);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        List<GoodBean> list = GoodBeanUtils.getInstance().getDaoSession().getGoodBeanDao().queryBuilder().list();
        rvCar.setAdapter(adapter);
        adapter.notifyDataSetChanged();

    }

    /**
     * 用于支付宝支付业务的入参 app_id。
     */
    public static final String APPID = "2021000117699214";

    /**
     * 用于支付宝账户登录授权业务的入参 pid。
     */
    public static final String PID = "";

    /**
     * 用于支付宝账户登录授权业务的入参 target_id。
     */
    public static final String TARGET_ID = "";

    /**
     * pkcs8 格式的商户私钥。
     * <p>
     * 如下私钥，RSA2_PRIVATE 或者 RSA_PRIVATE 只需要填入一个，如果两个都设置了，本 Demo 将优先
     * 使用 RSA2_PRIVATE。RSA2_PRIVATE 可以保证商户交易在更加安全的环境下进行，建议商户使用
     * RSA2_PRIVATE。
     * <p>
     * 建议使用支付宝提供的公私钥生成工具生成和获取 RSA2_PRIVATE。
     * 工具地址：https://doc.open.alipay.com/docs/doc.htm?treeId=291&articleId=106097&docType=1
     */
    public static final String RSA2_PRIVATE = "MIIEuwIBADANBgkqhkiG9w0BAQEFAASCBKUwggShAgEAAoIBAQCFU1L5Jdh2YZa9CuhY92lXRhCTVQiLoacN2H1XybIOA7pVstWGpn5tXUxyHBSUfMHbHmmG6i4WMm1511+qsSAdATa5dZL8nGQzJ7AwjXN6Iq0bbPBsrB8WrAY2ORnWWF5NTO5weLFtx6WZ2vLm37/DJKdU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wM98D5wKBgD22ir/CET12l+ya0GO1FCpI0Fgc8scWS1ht1b2p4aE3KnJYmBBmbDuHaGj989kGzmMeOnQyJaQhVmLociPfkVkoFPz6wXLPCrYRbp2u4HGE73w39fnSbtI1fCwBono5YEekgIr2wj9iHiUzOoWDRTCITD6O7VDbp68SMHTCmRAtAoGBAN71/mXkmubvG5hjd74SSa3n0tzFJvWwCq0X8lkLnDqL+c5BW87q2dCJcXuSk5OJwTOE6n0KILbq/vHtaKfkV4QBdOzS8AQCDIY26zxauR4+fAhtMeBXjc4bjoHv6/6WRuCUZysBxYbOe7LEkEr1jmsN6SrYAGBRZlyPcAd31ccf";
    public static final String RSA_PRIVATE = "";

    private static final int SDK_PAY_FLAG = 1;
    private static final int SDK_AUTH_FLAG = 2;

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @SuppressWarnings("unused")
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SDK_PAY_FLAG: {
                    @SuppressWarnings("unchecked")
                    PayResult payResult = new PayResult((Map<String, String>) msg.obj);
                    /**
                     * 对于支付结果，请商户依赖服务端的异步通知结果。同步通知结果，仅作为支付结束的通知。
                     */
                    String resultInfo = payResult.getResult();// 同步返回需要验证的信息
                    String resultStatus = payResult.getResultStatus();
                    // 判断resultStatus 为9000则代表支付成功
                    if (TextUtils.equals(resultStatus, "9000")) {
                        // 该笔订单是否真实支付成功，需要依赖服务端的异步通知。
                        ToastUtils.showShort("支付成功");
                    } else {
                        // 该笔订单真实的支付结果，需要依赖服务端的异步通知。
                        ToastUtils.showShort("支付失败");
                    }
                    break;
                }

                default:
                    break;
            }
        }

        ;
    };

    /**
     * 支付宝支付业务示例
     */
    public void payV2() {
        if (TextUtils.isEmpty(APPID) || (TextUtils.isEmpty(RSA2_PRIVATE) && TextUtils.isEmpty(RSA_PRIVATE))) {
            return;
        }

        /*
         * 这里只是为了方便直接向商户展示支付宝的整个支付流程；所以Demo中加签过程直接放在客户端完成；
         * 真实App里，privateKey等数据严禁放在客户端，加签过程务必要放在服务端完成；
         * 防止商户私密数据泄露，造成不必要的资金损失，及面临各种安全风险；
         *
         * orderInfo 的获取必须来自服务端；
         */
        boolean rsa2 = (RSA2_PRIVATE.length() > 0);
        Map<String, String> params = OrderInfoUtil2_0.buildOrderParamMap(APPID, rsa2);
        String orderParam = OrderInfoUtil2_0.buildOrderParam(params);

        String privateKey = rsa2 ? RSA2_PRIVATE : RSA_PRIVATE;
        String sign = OrderInfoUtil2_0.getSign(params, privateKey, rsa2);
        final String orderInfo = orderParam + "&" + sign;

        final Runnable payRunnable = new Runnable() {

            @Override
            public void run() {
                PayTask alipay = new PayTask(GoodCarActivity.this);
                Map<String, String> result = alipay.payV2(orderInfo, true);
                Log.i("msp", result.toString());

                Message msg = new Message();
                msg.what = SDK_PAY_FLAG;
                msg.obj = result;
                mHandler.sendMessage(msg);
            }
        };

        // 必须异步调用
        Thread payThread = new Thread(payRunnable);
        payThread.start();
    }

}
